//package com.dyh.quickdemozwei.handReactor;
//
//import java.io.IOException;
//import java.net.InetSocketAddress;
//import java.nio.channels.SelectionKey;
//import java.nio.channels.Selector;
//import java.nio.channels.ServerSocketChannel;
//
//public class Dispacther implements Runnable{
//
//        private String host = "127.0.0.1";
//        private int port = 8080;
//
//        public final Selector selector;
//        public final ServerSocketChannel serverSocketChannel;
//
//        public Dispacther() throws IOException {
//            selector=Selector.open();
//            serverSocketChannel=ServerSocketChannel.open();
//            InetSocketAddress inetSocketAddress=new InetSocketAddress(this.host,this.port);
//            serverSocketChannel.socket().bind(inetSocketAddress);
//            serverSocketChannel.configureBlocking(false);
//
//            SelectionKey selectionKey=serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
//
//            selectionKey.attach(new Acceptor(this));
//        }
//
//
//        @Override
//        public void run() {
//            try {
//                while(!Thread.interrupted()){
//                    selector.select();
//                    Set<SelectionKey> selectionKeys= selector.selectedKeys();
//                    Iterator<SelectionKey> it=selectionKeys.iterator();
//                    while(it.hasNext()){
//                        SelectionKey selectionKey=it.next();
//                        dispatch(selectionKey);
//                        selectionKeys.clear();
//                    }
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//
//         void dispatch(SelectionKey key) {
//                Runnable r = (Runnable)(key.attachment());
//                if (r != null){
//                    r.run();
//                }
//            }
//    }